/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgModificarCliente.java
 *
 * Created on Oct 20, 2011, 1:52:48 AM
 */

package visual.Despacho;

import bentity.beCliente;
import bentity.beUsuario;
import blogic.blCliente;
import dao.daoLog;
import java.sql.Date;
import javax.swing.JOptionPane;
import visual.Seguridad.visualHelper;

/**
 *
 * @author giuliomv
 */
public class dlgModificarCliente extends javax.swing.JDialog {
    private beCliente cliente = null;
    private beUsuario usuario = null;
    /** Creates new form dlgModificarCliente */
    public dlgModificarCliente(pnfMantenimientoClientes parent, boolean modal, beUsuario usuario) {
        super((java.awt.Frame)null, modal);
        initComponents();
        this.usuario = usuario;
        int fila = parent.getTblClientes().getSelectedRow();
        String dato = "";
        for(int i=0; i<parent.getTblClientes().getColumnCount(); i++){
            dato = String.valueOf(parent.getTblClientes().getValueAt(fila, i));
            switch (i){
                case 0:
                    this.txtCodigo.setText(dato);
                    break;
                case 1:
                    this.txtRazonSocial.setText(dato);
                    break;
                case 2:
                    this.txtRuc.setText(dato);
                    break;
                case 3:
                    this.txtDireccion.setText(dato);
                    break;
                case 4:
                    this.txtTelefono.setText(dato);
                    break;
                case 5:
                    this.txtEmail.setText(dato);
                    break;
                case 6:
                    this.txtContacto.setText(dato);
                    break;
            }
        }
        this.cliente = new beCliente(Integer.parseInt(this.txtCodigo.getText()), this.txtRazonSocial.getText(), this.txtRuc.getText(), this.txtTelefono.getText(), this.txtEmail.getText(), this.txtContacto.getText(), Date.valueOf((String)parent.getTblClientes().getValueAt(fila,7)), beCliente.C_ACTIVO, this.txtDireccion.getText());
        this.cliente.setNombreContacto(this.txtContacto.getText());
        this.cliente.setEmail(this.txtEmail.getText());
        this.setBounds(0, 0, 500, 500);
        this.setResizable(false);
        visualHelper.centrarDialogoEnPantalla(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnModificar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblTitulo = new javax.swing.JLabel();
        pnlModificar = new javax.swing.JPanel();
        txtTelefono = new javax.swing.JTextField();
        txtDireccion = new javax.swing.JTextField();
        lblTelefono = new javax.swing.JLabel();
        lblDireccion = new javax.swing.JLabel();
        txtRuc = new javax.swing.JTextField();
        lblRazonSocial = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        lblRuc = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        txtRazonSocial = new javax.swing.JTextField();
        txtCodigo = new javax.swing.JTextField();
        lblCodigo = new javax.swing.JLabel();
        lblContacto = new javax.swing.JLabel();
        txtContacto = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thor Store - Modificación de cliente");
        setModal(true);
        getContentPane().setLayout(null);

        btnModificar.setBackground(new java.awt.Color(240, 235, 226));
        btnModificar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnModificar.setForeground(new java.awt.Color(0, 0, 0));
        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });
        getContentPane().add(btnModificar);
        btnModificar.setBounds(150, 420, 90, 27);

        btnCancelar.setBackground(new java.awt.Color(240, 235, 226));
        btnCancelar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnCancelar.setForeground(new java.awt.Color(0, 0, 0));
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar);
        btnCancelar.setBounds(270, 420, 90, 27);

        lblTitulo.setFont(new java.awt.Font("Dialog", 0, 36));
        lblTitulo.setForeground(new java.awt.Color(0, 0, 0));
        lblTitulo.setText("Modificación de cliente");
        getContentPane().add(lblTitulo);
        lblTitulo.setBounds(49, 22, 414, 43);

        pnlModificar.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Datos de cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 14), new java.awt.Color(0, 0, 0))); // NOI18N
        pnlModificar.setLayout(null);

        txtTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTelefonoKeyTyped(evt);
            }
        });
        pnlModificar.add(txtTelefono);
        txtTelefono.setBounds(144, 190, 220, 27);

        txtDireccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDireccionKeyTyped(evt);
            }
        });
        pnlModificar.add(txtDireccion);
        txtDireccion.setBounds(144, 150, 220, 27);

        lblTelefono.setFont(new java.awt.Font("Dialog", 0, 12));
        lblTelefono.setForeground(new java.awt.Color(0, 0, 0));
        lblTelefono.setText("Teléfono *");
        pnlModificar.add(lblTelefono);
        lblTelefono.setBounds(40, 190, 70, 15);

        lblDireccion.setFont(new java.awt.Font("Dialog", 0, 12));
        lblDireccion.setForeground(new java.awt.Color(0, 0, 0));
        lblDireccion.setText("Dirección *");
        pnlModificar.add(lblDireccion);
        lblDireccion.setBounds(40, 150, 80, 15);

        txtRuc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtRucKeyTyped(evt);
            }
        });
        pnlModificar.add(txtRuc);
        txtRuc.setBounds(144, 110, 220, 27);

        lblRazonSocial.setFont(new java.awt.Font("Dialog", 0, 12));
        lblRazonSocial.setForeground(new java.awt.Color(0, 0, 0));
        lblRazonSocial.setText("Razón social *");
        pnlModificar.add(lblRazonSocial);
        lblRazonSocial.setBounds(40, 70, 90, 15);

        txtEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtEmailKeyTyped(evt);
            }
        });
        pnlModificar.add(txtEmail);
        txtEmail.setBounds(144, 230, 220, 27);

        lblRuc.setFont(new java.awt.Font("Dialog", 0, 12));
        lblRuc.setForeground(new java.awt.Color(0, 0, 0));
        lblRuc.setText("RUC *");
        pnlModificar.add(lblRuc);
        lblRuc.setBounds(40, 110, 40, 15);

        lblEmail.setFont(new java.awt.Font("Dialog", 0, 12));
        lblEmail.setForeground(new java.awt.Color(0, 0, 0));
        lblEmail.setText("E-mail");
        pnlModificar.add(lblEmail);
        lblEmail.setBounds(40, 230, 37, 15);

        txtRazonSocial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtRazonSocialKeyTyped(evt);
            }
        });
        pnlModificar.add(txtRazonSocial);
        txtRazonSocial.setBounds(144, 70, 220, 27);

        txtCodigo.setEnabled(false);
        pnlModificar.add(txtCodigo);
        txtCodigo.setBounds(144, 30, 220, 27);

        lblCodigo.setFont(new java.awt.Font("Dialog", 0, 12));
        lblCodigo.setForeground(new java.awt.Color(0, 0, 0));
        lblCodigo.setText("Código");
        pnlModificar.add(lblCodigo);
        lblCodigo.setBounds(40, 30, 58, 15);

        lblContacto.setFont(new java.awt.Font("Dialog", 0, 12));
        lblContacto.setForeground(new java.awt.Color(0, 0, 0));
        lblContacto.setText("Contacto");
        pnlModificar.add(lblContacto);
        lblContacto.setBounds(40, 270, 80, 15);

        txtContacto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtContactoKeyTyped(evt);
            }
        });
        pnlModificar.add(txtContacto);
        txtContacto.setBounds(144, 270, 220, 27);

        getContentPane().add(pnlModificar);
        pnlModificar.setBounds(60, 70, 390, 330);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        // TODO add your handling code here:
        if (    !this.txtDireccion.getText().isEmpty() &&
                !this.txtRuc.getText().isEmpty() &&
                !this.txtRazonSocial.getText().isEmpty() &&
                !this.txtTelefono.getText().isEmpty()    ){
            if (this.txtRuc.getText().length() == 11){
                boolean guardarAntiguos = false;
                if (this.txtContacto.getText().isEmpty() || this.txtEmail.getText().isEmpty()){
                    int rslt = visualHelper.mostrarPreguntaSiNo("No ha especificado el contacto y/o el email. ¿Desea mantener los datos antiguos?");
                    if (rslt == JOptionPane.YES_OPTION)
                        guardarAntiguos = true;
                }
                String strRazonSocial = "";
                String strRuc = "";
                String strTelefono = "";
                String strEmail = "";
                String strContacto = "";
                String strDireccion = "";
                Date fechaInscripcion = null;
                try{
                    int codCliente = this.cliente.getIdCliente();
                    strRazonSocial = this.txtRazonSocial.getText();
                    strRuc = this.txtRuc.getText();
                    strTelefono = this.txtTelefono.getText();
                    strEmail = this.txtEmail.getText();
                    if (strEmail.isEmpty()){
                        if (guardarAntiguos)
                            strEmail = this.cliente.getEmail();
                        else
                            strEmail = "Sin especificar";
                    }
                    strContacto = this.txtContacto.getText();
                    if (strContacto.isEmpty()){
                        if (guardarAntiguos)
                            strContacto = this.cliente.getNombreContacto();
                        else
                            strContacto = "Sin especificar";
                    }
                    fechaInscripcion = this.cliente.getFechaInscripcion();
                    strDireccion = this.txtDireccion.getText();
                    beCliente cliente = new beCliente(codCliente, strRazonSocial, strRuc, strTelefono, strEmail, strContacto, fechaInscripcion, beCliente.C_ACTIVO, strDireccion);
                    blCliente blCliente = new blCliente();
                    blCliente.modificarCliente(cliente);
                    visualHelper.mostrarMensajeSatisfaccion("Se modificó satisfactoriamente el cliente "+this.cliente.getRazonSocial());
                    (new daoLog()).registrarLog(this.usuario.getIdUsuario(), this.usuario.getCargo(), "Se modificó el cliente con código"+this.cliente.getIdCliente());
                    this.dispose();
                }
                catch (Exception e){
                    e.printStackTrace();
                    visualHelper.mostrarMensajeError("No se pudo modificar el cliente.");
                }
            }
            else
                visualHelper.mostrarMensajeAdvertencia("El RUC debe ser de 11 dígitos.");
        }
        else
            visualHelper.mostrarMensajeAdvertencia("Debe llenar los campos obligatorios (*).");
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        int rslt = visualHelper.mostrarPreguntaSiNo("Los cambios no se guardarán. ¿Desea salir de todos modos?");
        if (rslt == JOptionPane.YES_OPTION){
            this.setVisible(false);
            this.dispose();
        }
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtRazonSocialKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRazonSocialKeyTyped
        // TODO add your handling code here:
        String cad = this.txtRazonSocial.getText();
        if (cad.length() <= 50)
            visualHelper.soloNombres(evt);
        else
            evt.consume();
    }//GEN-LAST:event_txtRazonSocialKeyTyped

    private void txtRucKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRucKeyTyped
        // TODO add your handling code here:
        String strRuc = this.txtRuc.getText();
        if (strRuc.length() == 11)
            visualHelper.ningunCaracterEnTextField(evt);
        else
            visualHelper.soloDigitosEnTextField(evt);
    }//GEN-LAST:event_txtRucKeyTyped

    private void txtDireccionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDireccionKeyTyped
        // TODO add your handling code here:
        String cad = this.txtDireccion.getText();
        if (cad.length() <= 50)
            visualHelper.soloDigitosLetrasPuntoComaComilla(evt);
        else
            evt.consume();
    }//GEN-LAST:event_txtDireccionKeyTyped

    private void txtTelefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTelefonoKeyTyped
        // TODO add your handling code here:
        String cad = this.txtTelefono.getText();
        if (cad.length() <= 15)
            visualHelper.validarTelefono(evt);
        else
            evt.consume();
    }//GEN-LAST:event_txtTelefonoKeyTyped

    private void txtEmailKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtEmailKeyTyped
        // TODO add your handling code here:
        String cad = this.txtEmail.getText();
        if (cad.length() <= 20)
            visualHelper.soloEmail(evt);
        else
            evt.consume();
    }//GEN-LAST:event_txtEmailKeyTyped

    private void txtContactoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtContactoKeyTyped
        // TODO add your handling code here:
        String cad = this.txtContacto.getText();
        if (cad.length() <= 20)
            visualHelper.soloNombres(evt);
        else
            evt.consume();
    }//GEN-LAST:event_txtContactoKeyTyped

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblContacto;
    private javax.swing.JLabel lblDireccion;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblRazonSocial;
    private javax.swing.JLabel lblRuc;
    private javax.swing.JLabel lblTelefono;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JPanel pnlModificar;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtContacto;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtRazonSocial;
    private javax.swing.JTextField txtRuc;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables

}
